define(['knockout', 'util', 'bootstrap-treeview'], function (ko, Util) {
    function ViewModel(params) {
        this.small = false;

        Util.extend(this, params);

        var scope = this;

        scope._css = {'xgx-tree-sm':scope.small};

        //if (!scope.id) {
        scope._id = 'tree_{0}'.formatTpl(Util.uuid(8,16));
        //}
        scope.load = function () {
            if (scope.store&&scope.store.url) {
                Util.ajaxGet({url:scope.store.url, callback: function (data) {
                    if(scope.dataFormat){
                        Util.treeFormat(data.data,scope.dataFormat);
                    }

                    $('#' + scope._id).treeview($.extend({
                        data: data.data,
                        levels:1,
                        onNodeSelected: function (event, data) {
                            if(scope.onNodeSelected){
                                scope.onNodeSelected(data);
                            }
                        },
                        onNodeUnselected: function (event, data) {
                            if(scope.onNodeUnselected){
                                scope.onNodeUnselected(data);
                            }
                        }
                    },Util.treeConfig));
                }});
            }
        };

        scope.load();

        if(scope.store&&scope.store.id){
            ko.root.storeCache[scope.store.id] = {load:scope.load};
        }
    }

    return ViewModel;
});
